<?php
/**
 * Description of LayoutPicker
 *  Set the layout file the priority are
 *
 *  - {moduleName}-{controllerName}-{acctionName}.phtml
 *  - {moduleName}-{controllerName}.phtml
 *  - {moduleName}.phtml
 *  - default.phtml
 *
 * @author pdelavega
 */
class App_Controller_Plugin_LayoutPicker extends Zend_Controller_Plugin_Abstract
{
    
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {

        $layoutPath = Zend_Layout::getMvcInstance()->getLayoutPath();
        $moduleName = $request->getModuleName();
        $controllerName = $request->getControllerName();
        $actionName = $request->getActionName();
        
        if(file_exists($layoutPath.'/'.$moduleName.'-'.$controllerName.'-'.$actionName.'.phtml'))
        {
            Zend_Layout::getMvcInstance()->setLayout($moduleName.'-'.$controllerName.'-'.$actionName);
        }
        else if(file_exists($layoutPath.'/'.$moduleName.'-'.$controllerName.'.phtml'))
        {
            Zend_Layout::getMvcInstance()->setLayout($moduleName.'-'.$controllerName);
        }
        else if(file_exists($layoutPath.'/'.$moduleName.'.phtml'))
        {
            Zend_Layout::getMvcInstance()->setLayout($moduleName);
        }
        else
        {
            Zend_Layout::getMvcInstance()->setLayout('default');
        }
        
    }

}